package org.kong.controller;

import org.kong.entity.Emp;
import org.kong.service.EmpService;
import org.kong.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 加油
 *
 * @author konglingfeng
 * @date 2025/8/8
 * @desc
 */
@RestController
public class EmpController {

    @Autowired
    private EmpService empService;

    /**
     * 通过empno查询emp
     * @param empno
     * @return
     */
    @GetMapping("/emp/{empno}")
    public Result<Emp> findEmpById(@PathVariable int empno) {
        Emp emp = empService.findEmpById(empno);
        if (emp == null) {
            return Result.error("未查到改用户");
        }
        return Result.success("查找成功",emp);
    }

    /**
     * 查询全部emp
     * @return
     */
    @GetMapping("/emp/list")
    public Result<List<Emp>> findAllEmp() {
        List<Emp> emp = empService.findAllEmp();
        return Result.success("查找成功",emp);
    }

    /**
     * 更新emp
     * @param empno
     * @param emp
     * @return
     */
    @PostMapping("/emp/update/{empno}")
    public Result<Emp> updateEmp(@PathVariable int empno, @RequestBody Emp emp) {
        emp.setEmpno(empno);
        boolean isSuccess = empService.updateEmp(emp);
        if (isSuccess) {
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    /**
     * 添加emp
     * @param emp
     * @return
     */
    @PostMapping("/emp/add")
    public Result<Emp> addEmp(@RequestBody Emp emp) {
        boolean isSuccess = empService.addEmp(emp);
        if (isSuccess) {
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    /**
     * 删除emp
     * @param empno
     * @return
     */
    @DeleteMapping("/user/delete/{empno}")
    public Result<Emp> deleteEmp(@PathVariable int empno) {
        boolean isSuccess = empService.deleteEmp(empno);
        if (isSuccess) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

}
